using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using GameStateManager;
using Microsoft.Xna.Framework.Input;
using ScreenTest.Screens;
using ScreenTest.Projectiles;
using ScreenTest.Constants;

namespace ScreenTest.Players
{
    class Player
    {
        int health;
        int rocketAmmo;

        Vector2 position;
        Vector2 direction;
        Vector2 velocity;
        GamePlayScreen gameplayScreen;
        int playerID;
        int score;
        bool isAlive;
        int currentWeapon;

        float fireDelay;        //How much delay to apply between shots
        float fireTime;         //Time since last shot

        const int PLAYER_SIZE = 64;
        const int MAX_ROCKETS = GameConstants.MAX_ROCKETS;

        public Vector2 Center
        {
            get { return new Vector2(position.X + PLAYER_SIZE / 2, position.Y + PLAYER_SIZE / 2); }
        }

        public int CurrentWeapon
        {
            get { return currentWeapon; }
        }

        public bool IsAlive
        {
            get { return isAlive; }
        }

        public int ID
        {
            get { return playerID; }
        }

        public int Score
        {
            get { return score; }
        }

        public int Health
        {
            get { return health; }
            set { health = value; }
        }

        public int Rockets
        {
            get { return rocketAmmo; }
            set { rocketAmmo = value; }
        }

        public int Size
        {
            get { return PLAYER_SIZE; }
        }

        public Vector2 Direction
        {
            get { return direction; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public BoundingBox GetBoundingBox()
        {
            Vector3 min, max;

            //min = new Vector3(position.X - PLAYER_SIZE / 2, position.Y - PLAYER_SIZE / 2, 0);
            //max = new Vector3(position.X + PLAYER_SIZE / 2, position.Y + PLAYER_SIZE / 2, 0);

            min = new Vector3(position.X,position.Y, 0);
            max = new Vector3(position.X + PLAYER_SIZE, position.Y + PLAYER_SIZE, 0);

            return new BoundingBox(min, max);
        }

        public Player(int playerID, GamePlayScreen gameplayScreen, Vector2 position, int leftRight)
        {
            this.playerID = playerID;
            this.gameplayScreen = gameplayScreen;
            health = 50;
            this.position = position;
            direction = Vector2.Zero;
            rocketAmmo = 0;
            currentWeapon = GameConstants.WEAPON_NORMAL;
            fireTime = 0f;
            fireDelay = GameConstants.NORMAL_DELAY;
            
            isAlive = true;
        }

        public void Respawn(Vector2 location)
        {
            isAlive = true;
            position = location;
            velocity = Vector2.Zero;
            health = 100;
            rocketAmmo = 0;
        }

        public void Update()
        {
            position += velocity;
            velocity = Vector2.Zero;
        }

        public void HandleInput(InputState input, GameTime gameTime)
        {
            fireTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

            //if (playerID == 0)
            direction = input.GetPlayerGamePad(playerID).ThumbSticks.Right;
            direction.Y *= -1f;

            Vector2 move = input.GetPlayerGamePad(playerID).ThumbSticks.Left;
            move.Y *= -1f;
            

            //Another test
            if (input.IsKeyDown(Keys.S))
                move.X = -1f;
            if (input.IsKeyDown(Keys.F))
                move.X = 1f;
            if (input.IsKeyDown(Keys.E))
                move.Y = -1f;
            if (input.IsKeyDown(Keys.D))
                move.Y = 1f;

            velocity = move * 4f;

            /* Checking for weapon switch */
            GamePadState gpadState = input.GetPlayerGamePad(playerID);
            if (gpadState.Buttons.B == ButtonState.Pressed)
            {
                if (rocketAmmo > 0)
                {
                    SwitchWeapon(GameConstants.WEAPON_ROCKETS);
                }
            }

            if (gpadState.Buttons.A == ButtonState.Pressed)
            {
                SwitchWeapon(GameConstants.WEAPON_NORMAL);
            }
                
            if (direction.Length() > 0 && fireTime >= fireDelay)
            {
                fireTime = 0f;

                if (currentWeapon == GameConstants.WEAPON_NORMAL)
                    FireWeapon();
                else if (currentWeapon == GameConstants.WEAPON_ROCKETS)
                    FireRocket();
            }

            //position += move * 4f;

            /* For testing only */
            //direction = new Vector2(0, 0);
            //Vector2 move = new Vector2(0, 0);

            //if (playerID == 0)
            //{
            //    if (input.IsKeyDown(Keys.S))
            //    {
            //        move.X = -1f;
            //    }

            //    if (input.IsKeyDown(Keys.F))
            //    {
            //        move.X = 1f;
            //    }

            //    if (input.IsKeyDown(Keys.E))
            //    {
            //        move.Y = -1f;
            //    }

            //    if (input.IsKeyDown(Keys.D))
            //    {
            //        move.Y = 1f;
            //    }

            //    if (input.IsNewKeyPress(Keys.Enter))
            //    {
            //        direction.X = 1f;
            //        //direction.Y = 1f;
            //    }
            //}

            ////move.Normalize();
            //move *= 4f;
            //velocity = move;
            /* End of testing */

            
        }

        void SwitchWeapon(int weaponSwitchTo)
        {
            if (currentWeapon != weaponSwitchTo)
            {
                switch (weaponSwitchTo)
                {
                    case GameConstants.WEAPON_NORMAL:
                        fireDelay = GameConstants.NORMAL_DELAY;
                        break;

                    case GameConstants.WEAPON_ROCKETS:
                        fireDelay = GameConstants.ROCKET_DELAY;
                        break;
                }

                currentWeapon = weaponSwitchTo;
            }
        }

        public void FireWeapon()
        {
            //direction = new Vector2(rnd.Next(1, 10), rnd.Next(1, 10));
            gameplayScreen.FireRegularWeapon(playerID, Center, direction, 4f);
        }

        public void FireRocket()
        {
            if (rocketAmmo > 0)
            {
                rocketAmmo--;
                gameplayScreen.FireRocket(playerID, Center, direction, 10f);
            }
        }

        public void RecordHit()
        {
            score += 2;
        }

        public void HitByProjectile(Projectile projectile)
        {
            health -= 10;

            if (health <= 0)
            {
                health = 0;
                isAlive = false;
            }
        }
    }
}
